<?php

include_once LIBRARIES_TOOLS_XTEMPLATE;
include_once LIBRARIES_ENTITIES_DATABASE;
include_once LIBRARIES_ENTITIES_CATEGORIES;
include_once LIBRARIES_TOOLS_UTILITY;

class ContentController {

    public function invoke() {

        // soft
        $soft = $this->getArticleCategoryByID(193);
        // website
        $website = $this->getArticleCategoryByID(194);
        // service
        $service = $this->getArticleCategoryByID(195);

        $dir = dirname(__FILE__);

        // đường dẫn file view
        $xtemplate = new XTemplate($dir . '/views/content.html');

        if ($soft != null) {

            $xtemplate->assign('IMAGE1', $soft->getImage());
            $xtemplate->assign('TITLE1', $soft->getTitle());
            $xtemplate->assign('DESCRIPTION1', Utility::subString($soft->getDescription(), 150, true));
            $xtemplate->assign('ID1', $soft->getID());
            $xtemplate->assign('ALIAS1', $soft->getAlias());
            $xtemplate->parse("main.category1");
        }

        if ($website != null) {

            $xtemplate->assign('IMAGE2', $website->getImage());
            $xtemplate->assign('TITLE2', $website->getTitle());
            $xtemplate->assign('DESCRIPTION2', Utility::subString($website->getDescription(), 150, true));
            $xtemplate->assign('ID2', $website->getID());
            $xtemplate->assign('ALIAS2', $website->getAlias());
            $xtemplate->parse("main.category2");
        }
        if ($service != null) {

            $xtemplate->assign('IMAGE3', $service->getImage());
            $xtemplate->assign('TITLE3', $service->getTitle());
            $xtemplate->assign('DESCRIPTION3', Utility::subString($service->getDescription(), 130, true));
            $xtemplate->assign('ID3', $service->getID());
            $xtemplate->assign('ALIAS3', $service->getAlias());
            $xtemplate->parse("main.category3");
        }

        $xtemplate->parse("main");
        $xtemplate->out("main");
    }

    // tải danh sách ảnh theo mã gallery
    private function getArticleCategoryByID($ID) {
        $db = new Database();
        try {
            $category = new Categories();
            $category->setID($ID);

            $result = $category->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}

?>
